var express = require('express');
var router = express.Router();
const { uploadFile } = require('../utils/upload.js');
const filmApi = require('../api/film.js');
//内置模块
const path = require("path");
const fs = require("fs");
const until = require("util");
const unlink = until.promisify(fs.unlink);
/* GET home page. */
//添加图片
router.post('/upload', async function (req, res, next) {
    const { data } = await uploadFile(req);
    const result = await filmApi.addImg(data);
    res.send(result);
});

//删除图片
router.patch('/:_id', async function (req, res, next) {
    const { _id } = req.params;

    const { url } = req.body;

    const fileUrl = path.join(path.resolve(__dirname, "../public"), url);
    //删除文件
    await unlink(fileUrl);
    const data = await filmApi.removeImg(
        {
            _id, url
        }
    );
    res.send(data);
});

module.exports = router;
